#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
int			child_run = 0;
int			parent_run = 0;

void sig_child(int signum)
{
	if(SIGUSR1 == signum)
	{
		printf("i am child sig\n");
                child_run = 1;
	}
}

void sig_parent(int signum)
{
	if(SIGUSR2 == signum)
	{
               printf("i am parent sig\n");
               parent_run = 1;
	}
}


int main(int argc, char **argv)
{
	pid_t				pid = -1;
        int			        child_statu;

	//注册信号
        signal(SIGUSR1,sig_child);
	signal(SIGUSR2,sig_parent);

	//创建进程
        pid = fork();
	if(pid < 0)
	{
		printf("creat fork failure\n");
		exit(0);
	}
       
        //子进程
        if(pid == 0)
	{
                printf("creat child fork successful，wait signal..\n");

                while(!child_run)     //阻塞等待父进程发信号
                {
                      sleep(1);

                }
                kill(getppid(),SIGUSR2);//子进程给父进程发运行信号
                
                return 0;
	}
      	else
	{
               
                printf("wait child_fork give signal...\n");
                 
		kill(pid,SIGUSR1);      //给子进程发信号

                while(!parent_run )       //阻塞等待子进程发信号
                {
                      sleep(1);
                }

                printf("parent_fork start\n");
                
               
                waitpid(&child_statu);//父进程等待子进程退出，避免僵死进程
	}

	return 0;
}
